//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package javax.vpncrypto;

import java.io.IOException;
import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Provider;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import sun.security.util.DerInputStream;
import sun.security.util.DerOutputStream;
import sun.security.util.DerValue;
import sun.security.x509.AlgorithmId;

public class EncryptedPrivateKeyInfo {
    private AlgorithmId algid;
    private byte[] encryptedData;
    private byte[] encoded = null;

    public EncryptedPrivateKeyInfo(byte[] var1) throws IOException {
        if (var1 == null) {
            throw new NullPointerException("the encoded parameter must be non-null");
        } else {
            this.encoded = (byte[])var1.clone();
            DerValue var2 = new DerValue(this.encoded);
            DerValue[] var3 = new DerValue[]{var2.data.getDerValue(), var2.data.getDerValue()};
            if (var2.data.available() != 0) {
                throw new IOException("overrun, bytes = " + var2.data.available());
            } else {
                this.algid = AlgorithmId.parse(var3[0]);
                if (var3[0].data.available() != 0) {
                    throw new IOException("encryptionAlgorithm field overrun");
                } else {
                    this.encryptedData = var3[1].getOctetString();
                    if (var3[1].data.available() != 0) {
                        throw new IOException("encryptedData field overrun");
                    }
                }
            }
        }
    }

    public EncryptedPrivateKeyInfo(String var1, byte[] var2) throws NoSuchAlgorithmException {
        if (var1 == null) {
            throw new NullPointerException("the algName parameter must be non-null");
        } else {
            this.algid = AlgorithmId.get(var1);
            if (var2 == null) {
                throw new NullPointerException("the encryptedData parameter must be non-null");
            } else if (var2.length == 0) {
                throw new IllegalArgumentException("the encryptedData parameter must not be empty");
            } else {
                this.encryptedData = (byte[])var2.clone();
                this.encoded = null;
            }
        }
    }

    public EncryptedPrivateKeyInfo(AlgorithmParameters var1, byte[] var2) throws NoSuchAlgorithmException {
        if (var1 == null) {
            throw new NullPointerException("algParams must be non-null");
        } else {
            this.algid = AlgorithmId.get(var1);
            if (var2 == null) {
                throw new NullPointerException("encryptedData must be non-null");
            } else if (var2.length == 0) {
                throw new IllegalArgumentException("the encryptedData parameter must not be empty");
            } else {
                this.encryptedData = (byte[])var2.clone();
                this.encoded = null;
            }
        }
    }

    public String getAlgName() {
        return this.algid.getName();
    }

    public AlgorithmParameters getAlgParameters() {
        return this.algid.getParameters();
    }

    public byte[] getEncryptedData() {
        return (byte[])this.encryptedData.clone();
    }

    public PKCS8EncodedKeySpec getKeySpec(javax.vpncrypto.Cipher var1) throws InvalidKeySpecException {
        Object var2 = null;

        byte[] var5;
        try {
            var5 = var1.doFinal(this.encryptedData);
            checkPKCS8Encoding(var5);
        } catch (IOException | IllegalStateException | GeneralSecurityException var4) {
            throw new InvalidKeySpecException("Cannot retrieve the PKCS8EncodedKeySpec", var4);
        }

        return new PKCS8EncodedKeySpec(var5);
    }

    private PKCS8EncodedKeySpec getKeySpecImpl(Key var1, Provider var2) throws NoSuchAlgorithmException, InvalidKeyException {
        Object var3 = null;

        byte[] var8;
        try {
            javax.vpncrypto.Cipher var4;
            if (var2 == null) {
                var4 = javax.vpncrypto.Cipher.getInstance(this.algid.getName());
            } else {
                var4 = Cipher.getInstance(this.algid.getName(), var2);
            }

            var4.init(2, var1, this.algid.getParameters());
            var8 = var4.doFinal(this.encryptedData);
            checkPKCS8Encoding(var8);
        } catch (NoSuchAlgorithmException var6) {
            throw var6;
        } catch (IOException | GeneralSecurityException var7) {
            throw new InvalidKeyException("Cannot retrieve the PKCS8EncodedKeySpec", var7);
        }

        return new PKCS8EncodedKeySpec(var8);
    }

    public PKCS8EncodedKeySpec getKeySpec(Key var1) throws NoSuchAlgorithmException, InvalidKeyException {
        if (var1 == null) {
            throw new NullPointerException("decryptKey is null");
        } else {
            return this.getKeySpecImpl(var1, (Provider)null);
        }
    }

    public PKCS8EncodedKeySpec getKeySpec(Key var1, String var2) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException {
        if (var1 == null) {
            throw new NullPointerException("decryptKey is null");
        } else if (var2 == null) {
            throw new NullPointerException("provider is null");
        } else {
            Provider var3 = Security.getProvider(var2);
            if (var3 == null) {
                throw new NoSuchProviderException("provider " + var2 + " not found");
            } else {
                return this.getKeySpecImpl(var1, var3);
            }
        }
    }

    public PKCS8EncodedKeySpec getKeySpec(Key var1, Provider var2) throws NoSuchAlgorithmException, InvalidKeyException {
        if (var1 == null) {
            throw new NullPointerException("decryptKey is null");
        } else if (var2 == null) {
            throw new NullPointerException("provider is null");
        } else {
            return this.getKeySpecImpl(var1, var2);
        }
    }

    public byte[] getEncoded() throws IOException {
        if (this.encoded == null) {
            DerOutputStream var1 = new DerOutputStream();
            DerOutputStream var2 = new DerOutputStream();
            this.algid.encode(var2);
            var2.putOctetString(this.encryptedData);
            var1.write((byte)48, var2);
            this.encoded = var1.toByteArray();
        }

        return (byte[])this.encoded.clone();
    }

    private static void checkTag(DerValue var0, byte var1, String var2) throws IOException {
        if (var0.getTag() != var1) {
            throw new IOException("invalid key encoding - wrong tag for " + var2);
        }
    }

    private static void checkPKCS8Encoding(byte[] var0) throws IOException {
        DerInputStream var1 = new DerInputStream(var0);
        DerValue[] var2 = var1.getSequence(3);
        switch(var2.length) {
            case 4:
                checkTag(var2[3], (byte)-128, "attributes");
            case 3:
                checkTag(var2[0], (byte)2, "version");
                DerInputStream var3 = var2[1].toDerInputStream();
                var3.getOID();
                if (var3.available() != 0) {
                    var3.getDerValue();
                }

                checkTag(var2[2], (byte)4, "privateKey");
                return;
            default:
                throw new IOException("invalid key encoding");
        }
    }
}
